#include "ctrl.h"
#include "ui_ctrl.h"
#include "led.h"
#include "wendu.h"
#include "shidu.h"
#include "music.h"
#include "video.h"
#include "weather.h"
#include <QString>

ctrl::ctrl(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ctrl)
{
    ui->setupUi(this);

    /* 工人类实例化 */
    worker = new Worker;

    /* 将 worker 类移至线程 workerThread */
    worker->moveToThread(&workerThread);

    tcpSocket=new QTcpSocket(this);

    Tlink_connect();

    /* 信号槽连接 */
    /* 线程完成销毁对象 */
    connect(&workerThread, SIGNAL(finished()),worker, SLOT(deleteLater()));
    connect(&workerThread, SIGNAL(finished()),&workerThread, SLOT(deleteLater()));

    /* 发送开始工作的信号，开始工作 */
    connect(this, SIGNAL(startWork()),worker, SLOT(doWork1()));

    /* 接收到 worker 发送过来的信号 */
    connect(worker, SIGNAL(resultReady(QString)),this, SLOT(handleResults(QString)));

    workerThread.start();
    emit this->startWork();
}

ctrl::~ctrl()
{
    delete ui;
}

void ctrl::Tlink_connect()
{
    tcpSocket->connectToHost("112.74.142.132",8647);

    connect(tcpSocket, SIGNAL(connected()),this, SLOT(Tlink_connected()));
}

void ctrl::Tlink_connected()
{
    tcpSocket->write("NL2H61814901M027");
    connect(tcpSocket, SIGNAL(readyRead()),this, SLOT(receiveMessages()));
}

void ctrl::receiveMessages()
{
    QString messages = tcpSocket->readAll();

    if(messages=="1"){
        system("echo 1 > /sys/class/leds/sys-led/brightness");
    }
    else if(messages=="0"){
        system("echo 0 > /sys/class/leds/sys-led/brightness");
    }
}

void Worker::doWork1()
{
    /* 标志位为真 */
    isCanRun = true;

    /* 死循环 */
    while(1){
        /* 此{}作用是 QMutexLocker 与 lock 的作用范围，获取锁后，
         * 运行完成后即解锁 */
        {
            QMutexLocker locker(&lock);
            /* 如果标志位不为真 */
            if(!isCanRun){
                /* 跳出循环 */
                break;
            }
        }
        /* 使用 QThread 里的延时函数，当作一个普通延时 */
        QThread::sleep(5);

        emit resultReady("doWork1函数");
    }
    /* doWork1 运行完成，发送信号 */
    emit resultReady("打断doWork1函数");
}

void Worker::stopWork()
{
    qDebug()<<"打断线程"<<endl;

    /* 获取锁后，运行完成后即解锁 */
    QMutexLocker locker(&lock);
    isCanRun = false;
}

void ctrl::handleResults(const QString & results)
{
    int wendu=qrand()%10+20;
    int shidu=qrand()%100;
    QString data="#"+QString::number(wendu)+","+QString::number(shidu)+"#";

    tcpSocket->write(data.toUtf8().data());
    /* 打印线程的状态 */
    qDebug()<<"线程的状态:"<<results<<endl;
}

void ctrl::on_backbt_clicked()
{
    /* 停止耗时工作，跳出耗时工作的循环 */
    worker->stopWork();

    /* 断开连接 */
    tcpSocket->disconnectFromHost();

    /* 关闭socket*/
    tcpSocket->close();

    this->close();
}

void ctrl::on_ledbt_clicked()
{
    led *le=new led;
    le->setGeometry(this->geometry());
    le->show();
}

void ctrl::on_musicbt_clicked()
{
    music *mu=new music;
    mu->setGeometry(this->geometry());
    mu->show();
}

void ctrl::on_videobt_clicked()
{
    video *vi=new video;
    vi->setGeometry(this->geometry());
    vi->show();
}

void ctrl::on_wendubt_clicked()
{
    wendu *wd=new wendu;
    wd->setGeometry(this->geometry());
    wd->show();
}

void ctrl::on_shidubt_clicked()
{
    shidu *sd=new shidu;
    sd->setGeometry(this->geometry());
    sd->show();
}

void ctrl::on_weatherbt_clicked()
{
    weather *we=new weather;
    we->setGeometry(this->geometry());
    we->show();
}
